package cn.iocoder.yudao.module.pm.service.cooked;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.pplan.dal.dataobject.recipemeal.RecipeMealDO;
import cn.iocoder.yudao.module.pplan.dal.mysql.recipemeal.RecipeMealMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pm.controller.admin.cooked.vo.*;
import cn.iocoder.yudao.module.pm.dal.dataobject.cooked.CookedDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pm.dal.mysql.cooked.CookedMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.pm.enums.ErrorCodeConstants.*;

/**
 * 熟食成品管理 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class CookedServiceImpl implements CookedService {

    @Resource
    private CookedMapper cookedMapper;
    @Resource
    private RecipeMealMapper recipeMealMapper;

    @Override
    public Long createCooked(CookedSaveReqVO createReqVO) {
        // 插入
        CookedDO cooked = BeanUtils.toBean(createReqVO, CookedDO.class);
        RecipeMealDO recipeMealDO = recipeMealMapper.selectById(createReqVO.getMealId());
        cooked.setMealName(recipeMealDO.getMealName());
        cookedMapper.insert(cooked);
        // 返回
        return cooked.getId();
    }

    @Override
    public void updateCooked(CookedSaveReqVO updateReqVO) {
        // 校验存在
        validateCookedExists(updateReqVO.getId());
        // 更新
        CookedDO updateObj = BeanUtils.toBean(updateReqVO, CookedDO.class);
        RecipeMealDO recipeMealDO = recipeMealMapper.selectById(updateReqVO.getMealId());
        updateObj.setMealName(recipeMealDO.getMealName());
        cookedMapper.updateById(updateObj);
    }

    @Override
    public void deleteCooked(Long id) {
        // 校验存在
        validateCookedExists(id);
        // 删除
        cookedMapper.deleteById(id);
    }

    @Override
        public void deleteCookedListByIds(List<Long> ids) {
        // 校验存在
        validateCookedExists(ids);
        // 删除
        cookedMapper.deleteByIds(ids);
        }

    private void validateCookedExists(List<Long> ids) {
        List<CookedDO> list = cookedMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(COOKED_NOT_EXISTS);
        }
    }

    private void validateCookedExists(Long id) {
        if (cookedMapper.selectById(id) == null) {
            throw exception(COOKED_NOT_EXISTS);
        }
    }

    @Override
    public CookedDO getCooked(Long id) {
        return cookedMapper.selectById(id);
    }

    @Override
    public PageResult<CookedDO> getCookedPage(CookedPageReqVO pageReqVO) {
        return cookedMapper.selectPage(pageReqVO);
    }

}
